<template>
    <div>
        <NewsClass v-if="routeType == 1"></NewsClass>
        <GoodsClass v-if="routeType == 2"></GoodsClass>
    </div>
</template>

<script setup>
//0.加载页面依赖 start ---------------------------------------->
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
const channel_choice_1_num = ref(1)
const channel_choice_2_num = ref(1)
//0.加载页面依赖 end ---------------------------------------->

//1.获得路由id start ---------------------------------------->
const targetSegment = getRoutePath(1);
//1.1 获得当前的路由id
let routeId;
let routeType;
//通过导航路径反向查询导航id
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment,
    },
});
if (getRouteId.code == 200) {
    console.log('getRouteId', getRouteId.data);
    routeId = getRouteId.data.category_id
    routeType = getRouteId.data.type
} else {
    console.log("获得路由id出错！", getRouteId.message)
}
//1.获得路由id end ---------------------------------------->

//2.页面交互效果 start ---------------------------------------->
// 2.1 一级栏目
const title = ref("")
const tabsData = ref(1)

async function getPageDataTitle() {
    const mkdata = await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': routeId,
        },
    });
    if (mkdata.code == 200) {
        title.value = mkdata.data.alias;
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取二级标题")
        console.log("后端错误反馈：", mkdata.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
getPageDataTitle();
//2.页面交互效果 end ---------------------------------------->

//3.广告 start ---------------------------------------->
let adImg1 = ref({})
let adImg2 = ref({})
let adImg3 = ref({})
let adImg4 = ref({})
onMounted(async () => {
    //从客户端获取行政职能部门 加快打开速度
    const { $webUrl, $CwebUrl } = useNuxtApp();
    //广告1
    let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nfcpgxw_category_0001`
    const responseAd1 = await fetch(url, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd1 = await responseAd1.json();
    adImg1.value = resultAd1.data[0];

    //广告2
    let url2 = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nfcpgxw_category_0002`
    const responseAd2 = await fetch(url2, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd2 = await responseAd2.json();
    adImg2.value = resultAd2.data[0];

    //广告3
    let url3 = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nfcpgxw_category_0003`
    const responseAd3 = await fetch(url3, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd3 = await responseAd3.json();
    adImg3.value = resultAd3.data[0];

    //广告4
    let url4 = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nfcpgxw_category_0004`
    const responseAd4 = await fetch(url4, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd4 = await responseAd4.json();
    adImg4.value = resultAd4.data[0];
})
//3.广告 end ---------------------------------------->

//4.页面数据 start ---------------------------------------->
const pageData = ref([
    //示例：
    // {
    //     id: 1, 
    //     title: "", //该导航的标题
    //     data: [], //数据1
    //     data1: [], //数据2
    //     data2: [], //数据3
    //     data3: [], //数据4
    //     data4: [], //数据5
    //     category_id1: "",  //子导航id1
    //     category_id2: "", 
    //     category_id3: "",
    //     category_id4: "",
    //     title1: "", //子导航标题1
    //     title2: "",
    //     title3: "",
    //     title4: "",
    //     cid: "" //该导航的id
    // },
])

//4.1 获取所有导航
try {
    const navigateData = await requestDataPromise('/web/getWebsiteModelCategory', {
        method: 'GET',
        query: {
            'placeid': 1,
            'pid': routeId,
            'num': 20
        },
    });
    if (navigateData.code == 200) {
        // 遍历可用的导航池放到页面中
        for (let index in navigateData.data) {
            let data = {
                title: navigateData.data[index].name,
                cid: navigateData.data[index].category_id,
                children_count: navigateData.data[index].children_count,
                alias: navigateData.data[index].alias,
                aLIas_pinyin: targetSegment + "/" + navigateData.data[index].aLIas_pinyin,
                data: [],
                data1: [],
                data2: [],
                data3: [],
                data4: [],
                category_id1: "",
                category_id2: "",
                category_id3: "",
                category_id4: "",
                title1: "",
                title2: "",
                title3: "",
                title4: ""
            };
            if (navigateData.data[index].is_url == 1) {
                // 处理 URL 的逻辑
            } else {
                //每个页面最多8个模块
                pageData.value.push(data);
            }
        }
        //导航池加载完毕，开始申请模块数据
        let getJson = [
            // { "parent": routeId + ",0,0", "child": pageData.value[0].cid + ",4,8" },//模块1
            // { "parent": routeId + ",0,0", "child": pageData.value[1].cid + ",2,8" },//模块2
            // { "parent": routeId + ",0,0", "child": pageData.value[2].cid + ",1,6" },//模块3
            // { "parent": routeId + ",0,0", "child": pageData.value[3].cid + ",6,0" },//模块4
            // { "parent": routeId + ",0,0", "child": pageData.value[4].cid + ",2,3" },//模块5
            // { "parent": routeId + ",0,0", "child": pageData.value[5].cid + ",1,0" },//模块6
            // { "parent": routeId + ",0,0", "child": pageData.value[6].cid + ",3,10" },//模块7
            // { "parent": routeId + ",0,0", "child": pageData.value[7].cid + ",1,6" },//模块8
        ]
        for (let i = 0; i < pageData.value.length; i++) {
            if (i == 0) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[i].cid + ",4,8" });
            } else if (i == 1) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[i].cid + ",2,8" });
            } else if (i == 2) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[i].cid + ",1,6" });
            } else if (i == 3) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[i].cid + ",6,0" });
            } else if (i == 4) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[i].cid + ",2,3" });
            } else if (i == 5) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[i].cid + ",1,0" });
            } else if (i == 6) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[i].cid + ",3,10" });
            } else if (i == 7) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[i].cid + ",1,6" });
            }
        }
        let jsonString = JSON.stringify(getJson);
        getPageAllData(jsonString);

    } else {
        console.log("错误：渲染8个模块环节出错，请检查是否存在没有数据的模块！")
    }
} catch (error) {
    console.log("错误：导航池渲染执行接口出错！请检查频道页渲染的模块数据是否完整！")
}


async function getPageAllData(jsonString) {
    const mkdata = await requestDataPromise('/web/getWebsiteAllArticle', {
        method: 'GET',
        query: {
            'id': jsonString
        },
    });

    console.log("频道页渲染的模块数据：", mkdata);

    if (mkdata.code == 200) {
        if (mkdata.data.length > 0) {
            //模块1 顶部大图和列表
            pageData.value[0].data = mkdata.data[0].child.imgnum;
            pageData.value[0].data2 = mkdata.data[0].child.textnum;
        }
        if (mkdata.data.length > 1) {
            //模块2
            pageData.value[1].data = mkdata.data[1].child.imgnum;
            for (let index in mkdata.data[1].child.textnum) {
                if (index < 4) {
                    pageData.value[1].data2.push(mkdata.data[1].child.textnum[index]);
                } else {
                    pageData.value[1].data3.push(mkdata.data[1].child.textnum[index]);
                }
            }
        }
        if (mkdata.data.length > 2) {
            //模块3
            pageData.value[2].data = mkdata.data[2].child.imgnum;
            pageData.value[2].data2 = mkdata.data[2].child.textnum;
        }
        if (mkdata.data.length > 3) {
            //模块4
            pageData.value[3].data = mkdata.data[3].child.imgnum;
        }
        if (mkdata.data.length > 4) {
            //模块5
            pageData.value[4].data = mkdata.data[4].child.imgnum;
            pageData.value[4].data2 = mkdata.data[4].child.textnum;
        }
        if (mkdata.data.length > 5) {
            //模块6
            pageData.value[5].data = mkdata.data[5].child.imgnum;
        }
        if (mkdata.data.length > 6) {
            //模块7
            for (let index in mkdata.data[6].child.imgnum) {
                if (index < 1) {
                    pageData.value[6].data.push(mkdata.data[6].child.imgnum[index]);
                } else {
                    pageData.value[6].data2.push(mkdata.data[6].child.imgnum[index]);
                }
            }
            pageData.value[6].data3 = mkdata.data[6].child.textnum;
        }
        if (mkdata.data.length > 7) {
            //模块8
            pageData.value[7].data = mkdata.data[7].child.imgnum;
            pageData.value[7].data2 = mkdata.data[7].child.textnum;
        }

    } else {
        ElMessage.error(mkdata.message)
    }
}
//4.页面数据 end ---------------------------------------->

//5.设置seo信息 start---------------------------------------->
const setData = await requestDataPromise('/web/getWebsiteCategoryHead', {
    method: 'GET',
    query: {
        'catid': routeId
    },
});

if (setData.code == 200) {
    let seoTitle = setData.data.seo_title;
    let seoDescription = setData.data.seo_description;
    let seoKeywords = setData.data.seo_keywords;
    let seoSuffix = setData.data.suffix;
    let seoName = setData.data.website_name;

    useSeoMeta({
        title: seoTitle + "_" + seoSuffix,
        meta: [
            { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix, tagPriority: 10 },
            { name: 'description', content: seoDescription + "_" + seoName + "_" + seoSuffix, tagPriority: 10 }
        ]
    });
} else {
    console.log("设置频道页SEO出错！", setData.message)
}
//5.设置seo信息 end---------------------------------------->
</script>

<style lang="less" scoped>
@import '@/assets/css/class.less';
</style>